﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YBOT_Field_Control
{
    public partial class Score : Form
    {
        
        public bool finalScore = false;
        public bool manualOverride = false;
        Game sGame;

        public Score()
        {
            InitializeComponent();
        }

        public Score(Game _game)
        {
            InitializeComponent();
            this.sGame = _game;
        }

        private void Score_Shown(object sender, EventArgs e)
        {
            if(this.sGame.green.autoBonus) mtbGreenAutoBonus.Text = this.sGame.green.autoBonusValue.ToString();
            mtbGreenAutoLights.Text = this.sGame.green.autoBall.ToString();
            if(this.sGame.red.autoBonus) mtbRedAutoBonus.Text = this.sGame.red.autoBonusValue.ToString();
            mtbRedAutoLights.Text = this.sGame.red.autoBall.ToString();
            mtbGreenRows.Text = rowCalc("Green").ToString();
            mtbRedRows.Text = rowCalc("Red").ToString();
            mtbGreenEndLights.Text = lightCount("Green").ToString();
            mtbRedEndLights.Text = lightCount("Red").ToString();
            mtbGreenPenalty.Text = this.sGame.green.penalty.ToString();
            mtbRedPenalty.Text = this.sGame.red.penalty.ToString();
            if (this.sGame.green.dq) button1_Click(button1, EventArgs.Empty);
            if (this.sGame.red.dq) button2_Click(button2, EventArgs.Empty);

            updateFinalScore();
        }

        private void btnFinalScore_Click(object sender, EventArgs e)
        {

            updateFinalScore();

            this.sGame.green.score = this.sGame.green.finalScore;
            this.sGame.red.score = this.sGame.red.finalScore;

            this.finalScore = true;

        }

        private void mtbGreenAutoLights_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbGreenAutoBonus_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbGreenRows_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbGreenProtons_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbRedAutoLights_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbRedAutoBonus_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbRedRows_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void mtbRedProtons_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void updateFinalScore()
        {
            if (!manualOverride)
            {
                mtbRedRows.Text = rowCalc("Red").ToString();
                mtbGreenRows.Text = rowCalc("Green").ToString();
            }

            this.sGame.green.autoLightsScore = Convert.ToInt16(mtbGreenAutoLights.Text);
            this.sGame.green.autoBonusScore = Convert.ToInt16(mtbGreenAutoBonus.Text);
            this.sGame.green.midBonusScore = this.sGame.green.midFieldBonus;
            this.sGame.green.rowsSore = Convert.ToInt16(mtbGreenRows.Text);
            this.sGame.green.protonsScore = Convert.ToInt16(mtbGreenProtons.Text);
            this.sGame.green.endLightsScore = Convert.ToInt16(mtbGreenEndLights.Text);
            this.sGame.green.penalty = Convert.ToInt16(mtbGreenPenalty.Text);

            this.sGame.red.autoLightsScore = Convert.ToInt16(mtbRedAutoLights.Text);
            this.sGame.red.autoBonusScore = Convert.ToInt16(mtbRedAutoBonus.Text);
            this.sGame.red.midBonusScore = this.sGame.red.midFieldBonus;
            this.sGame.red.rowsSore = Convert.ToInt16(mtbRedRows.Text);
            this.sGame.red.protonsScore = Convert.ToInt16(mtbRedProtons.Text);
            this.sGame.red.endLightsScore = Convert.ToInt16(mtbRedEndLights.Text);
            this.sGame.red.penalty = Convert.ToInt16(mtbRedPenalty.Text);

            this.sGame.green.finalScore = this.sGame.green.autoBonusScore + (this.sGame.green.autoLightsScore * 5) + this.sGame.green.midBonusScore + (this.sGame.green.rowsSore * 10)
                + (this.sGame.green.protonsScore * 5) + (this.sGame.green.endLightsScore * 5);
            this.sGame.red.finalScore = this.sGame.red.autoBonusScore + (this.sGame.red.autoLightsScore * 5) + this.sGame.red.midBonusScore + (this.sGame.red.rowsSore * 10)
                + (this.sGame.red.protonsScore * 5) + (this.sGame.red.endLightsScore * 5);

            this.sGame.green.finalScore = (int)(this.sGame.green.finalScore - (this.sGame.green.penalty * 15));
            this.sGame.red.finalScore = this.sGame.red.finalScore - (this.sGame.red.penalty * 15);

            mtbGreenFinalScore.Text = this.sGame.green.finalScore.ToString();
            mtbRedFinalScore.Text = this.sGame.red.finalScore.ToString();
        }

        private int rowCalc(string team)
        {

            if (team == "Green")
            {
                bool middle;
                int rows = 0;
                if (btnGreenMiddleBonus.Text == "30" && btnRedMiddleBonus.Text != "30") middle = true;
                else middle = false;

                if (this.sGame.green.tower2 && this.sGame.green.tower1 && this.sGame.green.tower8) rows++;
                if (this.sGame.green.tower2 && this.sGame.green.tower3 && this.sGame.green.tower4) rows++;
                if (this.sGame.green.tower4 && this.sGame.green.tower5 && this.sGame.green.tower6) rows++;
                if (this.sGame.green.tower6 && this.sGame.green.tower7 && this.sGame.green.tower8) rows++;

                if (this.sGame.green.tower1 && middle && this.sGame.green.tower5) rows++;
                if (this.sGame.green.tower2 && middle && this.sGame.green.tower6) rows++;
                if (this.sGame.green.tower3 && middle && this.sGame.green.tower7) rows++;
                if (this.sGame.green.tower4 && middle && this.sGame.green.tower8) rows++;

                return rows;

            }
            else if (team == "Red")
            {
                bool middle;
                int rows = 0;
                if (btnGreenMiddleBonus.Text != "30" && btnRedMiddleBonus.Text == "30") middle = true;
                else middle = false;

                if (this.sGame.red.tower2 && this.sGame.red.tower1 && this.sGame.red.tower8) rows++;
                if (this.sGame.red.tower2 && this.sGame.red.tower3 && this.sGame.red.tower4) rows++;
                if (this.sGame.red.tower4 && this.sGame.red.tower5 && this.sGame.red.tower6) rows++;
                if (this.sGame.red.tower6 && this.sGame.red.tower7 && this.sGame.red.tower8) rows++;

                if (this.sGame.red.tower1 && middle && this.sGame.red.tower5) rows++;
                if (this.sGame.red.tower2 && middle && this.sGame.red.tower6) rows++;
                if (this.sGame.red.tower3 && middle && this.sGame.red.tower7) rows++;
                if (this.sGame.red.tower4 && middle && this.sGame.red.tower8) rows++;

                return rows;
            }
            else return 0;
        }

        private int lightCount(string team)
        {
            if (team == "Green")
            {
                int lights = 0;

                if(this.sGame.green.tower1) lights++;
                if(this.sGame.green.tower2) lights++;
                if(this.sGame.green.tower3) lights++;
                if(this.sGame.green.tower4) lights++;
                if(this.sGame.green.tower5) lights++;
                if(this.sGame.green.tower6) lights++;
                if(this.sGame.green.tower7) lights++;
                if(this.sGame.green.tower8) lights++;

                return lights;

            }
            else if (team == "Red")
            {
                int lights = 0;

                if(this.sGame.red.tower1) lights++;
                if(this.sGame.red.tower2) lights++;
                if(this.sGame.red.tower3) lights++;
                if(this.sGame.red.tower4) lights++;
                if(this.sGame.red.tower5) lights++;
                if(this.sGame.red.tower6) lights++;
                if(this.sGame.red.tower7) lights++;
                if(this.sGame.red.tower8) lights++;

                return lights;
            }
            else return 0;
        }

        private void btnGreenMiddleBonus_Click(object sender, EventArgs e)
        {
            if (btnGreenMiddleBonus.Text == "0")
            {
                this.sGame.green.midFieldBonus = 30;
                btnGreenMiddleBonus.Text = "30";
            }
            else
            {
                this.sGame.green.midFieldBonus = 0;
                btnGreenMiddleBonus.Text = "0";
            }
            updateFinalScore();
        }

        private void btnRedMiddleBonus_Click(object sender, EventArgs e)
        {
            if (btnRedMiddleBonus.Text == "0")
            {
                this.sGame.red.midFieldBonus = 30;
                btnRedMiddleBonus.Text = "30";
            }
            else
            {
                this.sGame.red.midFieldBonus = 0;
                btnRedMiddleBonus.Text = "0";
            }

            updateFinalScore();
        }

        private void btnUpdateScore_Click(object sender, EventArgs e)
        {
            updateFinalScore();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "")
            {
                button1.Text = "DQ'd";
                this.sGame.green.dq = true;
            }
            else
            {
                button1.Text = "";
                this.sGame.green.dq = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "")
            {
                button2.Text = "DQ'd";
                this.sGame.red.dq = true;
            }
            else
            {
                button2.Text = "";
                this.sGame.red.dq = false;
            }
        }

        private void btnOverride_Click(object sender, EventArgs e)
        {
            if (btnOverride.BackColor == DefaultBackColor)
            {
                btnOverride.BackColor = Color.Lime;
                manualOverride = true;
            }
            else
            {
                btnOverride.BackColor = DefaultBackColor;
                manualOverride = false;
            }
  
        }

    }
}
